var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();
Page({
  data: {
    content: {},
    contents: {},
    px: [{
        id: 1,
        name: '> 价格升序  ↑'
      },
      {
        id: 2,
        name: '> 价格降序  ↓'
      }
    ],
    pxs: [{
        id: 1,
        name: '> 销量升序  ↑'
      },
      {
        id: 2,
        name: '> 销量降序  ↓'
      }
    ],
    cId: 0,
    catePrice: 0,
    cateCount: 0,
    pxopen: false,
    pxshow: false,
    pxopens: false,
    pxshows: false,
    active: true,
    imgUrl: "../../static/images/down.png",
    navList: [],
    goodsList: [],
    id: 0,
    remindValue: ['', '', '送'],
    plusFlag: ['', '+', '  '],
    currentCategory: {},
    memberTypes: false,
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    size: 6,
    loadflag: 0,
    topNum: 0,
    show: false,
    height: '80%',
    shopWord: '',
    memberGoodsNum: [],
    integralGoodsNum: []
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if (options.id) {
      that.setData({
        id: parseInt(options.id),
        cId: parseInt(options.id)
      });
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

    this.getCategoryInfo();

  },
  getCategoryInfo: function() {


    let that = this;
    util.request(api.GoodsCategory, {
        id: this.data.id
      })
      .then(function(res) {
        if (res.errno == 0) {
          that.setData({
            navList: res.data.brotherCategory,
            currentCategory: res.data.currentCategory,
            memberGoodsNum: app.globalData.memberGoodsNum,
            integralGoodsNum: app.globalData.integralGoodsNum
          });

          wx.setNavigationBarTitle({
            title: res.data.parentCategory.name
          })

          //nav位置
          let currentIndex = 0;
          let navListCount = that.data.navList.length;

          if (that.data.loadflag == 0) {
            let queryId = that.data.navList[0].id;
            that.setData({
              id: queryId,
              loadflag: 1
            })
          }

          for (let i = 0; i < navListCount; i++) {
            currentIndex += 1;
            if (that.data.navList[i].id == that.data.id) {
              break;
            }
          }
          if (currentIndex > navListCount / 2 && navListCount > 5) {
            that.setData({
              scrollLeft: currentIndex * 60
            });
          }
          that.getGoodsList();

        } else {
          //显示错误信息
        }

      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示

    var that = this;
    that.setData({
      memberTypes: false
    })
    var memberType = false;
    if (app.globalData.hasLogin) {
      memberType = wx.getStorageSync("memberType")
    }

    util.request(api.GoodsCategory, {
        id: this.data.id
      })
      .then(function(res) {
        if (res.errno == 0) {
          var memberCategoryId = res.data.parentCategory.id;
          if (!memberType && memberCategoryId == 1036016) {
            that.setData({
              memberTypes: true
            })
          }
          if (that.data.memberTypes == true) {
            that.setData({
              show: true
            })
          }
        } else {
          //显示错误信息
        }
      });
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    var that = this;
    that.setData({
      size: this.data.size + 6
    });
    this.getGoodsList()
  },
  onHide: function() {
    // 页面隐藏
  },
  getGoodsList: function() {
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    util.request(api.GoodsLists, {
        cId: that.data.cId,
        categoryId: that.data.id,
        catePrice: that.data.catePrice,
        page: that.data.page,
        size: that.data.size,
        cateCount: that.data.cateCount
      })
      .then(function(res) {
        if (res.errno === 0) {
          that.setData({
            goodsList: res.data.goodsList,
            shopWord: res.data.shopWord,
          });
        }
        wx.hideLoading();
      });
  },
  onUnload: function() {
    // 页面关闭
  },
  joinMember: function() {
    var that = this;
    let memberType = false;
    if (app.globalData.hasLogin) {
      util.request(api.UserMemberType).then(function(res) {
        console.log(res.data.hasLogin)
        if (res.data.hasLogin == false) {
          app.globalData.hasLogin = false;
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.navigateTo({
            url: "/pages/auth/login/login"
          });
        } else {
          memberType = res.data.MemberType;
          if (memberType) {
            app.globalData.memberType = true;
            that.setData({
              memberTypes: true
            })
          } else {
            wx.navigateTo({
              url: "/pages/ucenter/memberRecharge/memberRecharge?amount=66&disable=true"
            });
          }
        }
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goTop: function(e) {
    this.setData({
      topNum: 0
    });
  },
  confirm: function() {
    wx.navigateTo({
      url: '/pages/ucenter/memberRecharge/memberRecharge?amount=66&disable=true',
    })
    this.setData({
      show: false
    })
  },
  back: function() {
    this.setData({
      show: false
    })
  },
  switchCate: function(event) {
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    this.setData({
      size: 6
    });
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id
    });
    this.getCategoryInfo();
  },


  listpx: function(e) {
    console.log(e)
    if (this.data.pxopen) {
      this.setData({
        pxopen: false,
        pxshow: false,
        active: true,
        imgUrl: "../../static/images/down.png"
      })
    } else {
      this.setData({
        content: this.data.px,
        pxopen: true,
        pxshow: false,
        active: false,
        imgUrl: "../../static/images/up.png"
      })
    }
    console.log(e.target)
  },
  goPrice: function(e) {
    var src = e.currentTarget.dataset.src;
    console.log(typeof src)
    this.setData({
      catePrice: src,
      pxopen: false,
      cateCount: 0
    })
    this.getGoodsList()
  },


  listpxs: function(e) {
    if (this.data.pxopens) {
      this.setData({
        pxopens: false,
        pxshows: false,
        active: true,
        imgUrl: "../../static/images/down.png"
      })
    } else {
      this.setData({
        contents: this.data.pxs,
        pxopens: true,
        pxshows: false,
        active: false,
        imgUrl: "../../static/images/up.png"
      })
    }
  },
  goCount: function(e) {
    var src = e.currentTarget.dataset.src;
    this.setData({
      cateCount: src,
      pxopens: false,
      catePrice: 0
    })
    this.getGoodsList()
  }
})